﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data;

namespace EasyBuildingCMS.BLL.CoreEngine
{
    public class MasterPageRepository:CoreEngineBaseRepository
    {
        public MasterPageRepository() {
            CacheKey += "_MasterPage";
        }

        #region Query

        public List<MasterPage> GetAcitveMasterPages()
        {
            string key = CacheKey + "_Active";

            if (EnableCaching && Cache[key] != null)
            {
                return (List<MasterPage>)Cache[key];
            }

            this.CoreEngineContext.MasterPage.MergeOption = MergeOption.NoTracking;
            
            List<MasterPage> pages = (from page in CoreEngineContext.MasterPage
                                       where page.Active
                                       select page).ToList();

            if (EnableCaching)
            {
                CacheData(key, pages, CacheDuration);
            }

            return pages;
        }

        public List<MasterPage> GetInacitveMasterPages()
        {
            string key = CacheKey + "_Inactive";

            if (EnableCaching && Cache[key] != null)
            {
                return (List<MasterPage>)Cache[key];
            }

            this.CoreEngineContext.MasterPage.MergeOption = MergeOption.NoTracking;

            List<MasterPage> pages = (from page in CoreEngineContext.MasterPage
                                where !page.Active
                                select page).ToList();

            if (EnableCaching)
            {
                CacheData(key, pages, CacheDuration);
            }

            return pages;
        }

       

        public MasterPage GetMasterPageByID(int pageID)
        {

            string key = string.Format(CacheKey + "_page_{0}", pageID);

            if (EnableCaching && (Cache[key] != null))
            {
                return (MasterPage)Cache[key];
            }

            this.CoreEngineContext.MasterPage.MergeOption = MergeOption.NoTracking;
            MasterPage page = this.CoreEngineContext.MasterPage.Where(p => p.MasterPageID == pageID).FirstOrDefault<MasterPage>();

            if (EnableCaching)
            {
                CacheData(key, page, CacheDuration);
            }

            return page;
        }

        #endregion

        #region Update

        #endregion

        #region Insert

        public MasterPage InsertMasterPage(int masterPageID, string masterPageFile, string masterPageFriendlyFile, string masterPageDescription, int sortOrder) 
        {

            MasterPage masterPage = MasterPage.CreateMasterPage(masterPageID, masterPageFile, true);
            masterPage.MasterPageFriendlyName = masterPageFriendlyFile;
            masterPage.MasterPageDescription = masterPageDescription;
            masterPage.SortOrder = sortOrder;

            return this.InsertMasterPage(masterPage);
        
        }
   
        public MasterPage InsertMasterPage(MasterPage page)
        {

            try
            {
                if (page.EntityState == EntityState.Detached)
                {
                    this.CoreEngineContext.AddToMasterPage(page);
                }
                if (base.SaveChange(page.MasterPageID)){
                    return page;
                }
                else{
                    return null;
                }
            }
            catch (Exception ex)
            {
                ActiveExceptions.Add(CacheKey + "_" + page.MasterPageID, ex);
                return null;

            }
        }

        #endregion
      
        #region " Delete Operations "

        public bool DeletePage(int masterPageId)
        {
            return ChangeDeletedState(this.GetMasterPageByID(masterPageId), false);
        }

        public bool DeleteArticle(MasterPage page)
        {
            return ChangeDeletedState(page, false);
        }

        public bool UnDeletePage(MasterPage page)
        {
            return ChangeDeletedState(page, true);
        }

        private bool ChangeDeletedState(MasterPage page, bool state)
        {
            page.Active = state;
            return base.SaveChange(page.MasterPageID);
        }

        #endregion

    }
}